/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package twi.result.graph;

import java.util.Iterator;
import java.util.List;

import prefuse.data.Edge;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Tree;
import prefuse.util.GraphLib;
import twi.OWLModel;
import twi.Utils;
import twi.filter.DropAnonFilter;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.rdf.model.RDFNode;

/**
 * 
 * @author Soeren
 */
public class PrefuseGraphFactory {

	public static Graph create(RGraph rg) {
		Graph g = new Graph(false);
		g.getNodeTable().addColumns(GraphLib.LABEL_SCHEMA);
		g.getEdgeTable().addColumns(GraphLib.LABEL_SCHEMA);
		
//		rg.removeRootFromMap();
		// Node root = g.addNode();
		// root.setString(GraphLib.LABEL, "root");
		Node root = null;
		if (rg.root != null){
			root = g.addNode();
			root.setString(GraphLib.LABEL, rg.root.getName());
			rg.root.setVisualNode(root);
		}

		Iterator<RNode> nodeIter = rg.getNodeIter();
		while (nodeIter.hasNext()) {
			RNode node = nodeIter.next();
//			if (node == rg.root)
//				continue;
			Node pn = g.addNode();
			pn.setString(GraphLib.LABEL, node.getName());
			node.setVisualNode(pn);
			 if (root != null && node.getInEdge().isEmpty())
				 g.addEdge(root, pn);
		}

		Iterator<REdge> eIter = rg.getEdgeIter();
		while (eIter.hasNext()) {
			REdge e = eIter.next();
			Node n1 = e.getOrigin().getVisualNode();
			Node n2 = e.getTarget().getVisualNode();
			if (n1 == null || n2 == null) {
				// System.err.println("Kein Knoten");
				continue;
			}
			Edge edge = g.addEdge(n1, n2);
			String name = e.getLabel();
			if (!name.contains("->"))
				edge.setString(GraphLib.LABEL, name);
		}
		return g;
	}

	public static Tree createGraph(OWLModel model) {
		Tree t = new Tree();
		t.getNodeTable().addColumn(GraphLib.LABEL, String.class);
		
		Node root = t.addRoot();
		root.setString(GraphLib.LABEL, "root");
		
		List rootList = model.listHierarchyRootClasses().filterDrop(new DropAnonFilter()).toList();
		for (int i = 0; i < rootList.size(); i++){
			String name = Utils.getLocalName((OntClass)rootList.get(i));
			Node n = t.addChild(root);
			n.setString(GraphLib.LABEL, name);
			rekursivAddSubNodes((OntClass)rootList.get(i), n, t);
		}
		
		return t;
	}

	private static void rekursivAddSubNodes(RDFNode ontClass, Node par, Tree t) {
		if (ontClass instanceof OntClass){
			List subList = ((OntClass)ontClass).listSubClasses().filterDrop(new DropAnonFilter()).toList();
			for (int i = 0; i < subList.size(); i++){
				Node n = t.addChild(par);
				n.setString(GraphLib.LABEL, Utils.getLocalName((RDFNode)subList.get(i)));
				rekursivAddSubNodes((RDFNode) subList.get(i), n, t);
			}
			List inList = ((OntClass)ontClass).listInstances().filterDrop(new DropAnonFilter()).toList();
			if (!inList.isEmpty()){
				Node in = t.addChild(par);
				in.setString(GraphLib.LABEL, "individuals");
//				Node in = par;
				for (int i = 0; i < inList.size(); i++){
					Node n = t.addChild(in);
					n.setString(GraphLib.LABEL, Utils.getLocalName((RDFNode) inList.get(i)));
				}	
			}
			
		}
	
	}

}
